<?php
error_reporting(0);

/**
 * Created by PhpStorm.
 * User: sunshubo
 * Date: 2017/9/26
 * Time: 14:23
 */
class MY_Model extends CI_Model
{
    function __construct()
    {
//        $this->CI->load->library('session');            //session verify
    }

    /**
     * @param $table
     * @param $dataArray
     * @return mixed
     */
    public function my_insert($table, $dataArray)
    {
        $this->db->insert($table, $dataArray);
        return $this->db->insert_id();
    }

    public function my_select($table, $where, $field = null)
    {
//        $sql = "";
//        if (is_array($where)) {
//            foreach ($where as $item => $value) {
//                if ($sql === '') {
//                    $sql = $item . " = '$value'";
//                } else {
//                    $sql = $sql . ' AND ' . $item . " = '$value'";
//                }
//            }
//            if (empty($field)) {
//                $sql = "select * from $table where " . $sql;
//            } else {
//                $sql = "select $field from $table where " . $sql;
//            }
//        }

        if ($where) {
            $this->db->where($where);
        }
        if ($field) {
            $this->db->select($field);
        }

        $sql = $this->db->get_compiled_select($table);
//        $sql = $this->db->select($field)->where($where)->get_compiled_select($table);
        $query = $this->db->query($sql);

        if ($field === 'count(*)') {
            return $query->num_rows();
        } elseif (!empty($field) && strpos($field, ',') <= 0) {
            if ($query->num_rows()) {
                $id = $query->result_array();
                return $id[0][$field];
            }
        }
        return $query->result_array();
    }

    public function my_select_num($table, $where)
    {
        $sql = "";
        if (is_array($where)) {
            foreach ($where as $item => $value) {
                if ($sql === '') {
                    $sql = $item . '=' . $value;
                } else {
                    $sql = $sql . ' AND ' . $item . '=' . $value;
                }
            }
            $sql = "select * from $table where " . $sql;
        }
        $query = $this->db->query($sql);
        return $query->num_rows();
    }

    public function my_update($table, $where, $set, $set1, $set2)
    {
//        $set_ = "";
//        $where_ = "";
//        if (is_array($set)) {
//            foreach ($set as $item => $value) {
//                if ($set_ === '') {
//                    $set_ = $item . '=' . $value;
//                } else {
//                    $set_ = $set_ . ' , ' . $item . '=' . $value;
//                }
//            }
//        }
//        if (is_array($where)) {
//            foreach ($where as $item => $value) {
//                if ($where_ === '') {
//                    $where_ = $item . '=' . $value;
//                } else {
//                    $where_ = $where_ . ' AND ' . $item . '=' . $value;
//                }
//            }
//        }
//        $sql = /** @lang text */
//            "update $table set " . $set_ . " where " . $where_;
////        var_dump($sql, $set_, $where_);
//
        if (gettype($set) === "string") {
            $sql = $this->db->set($set, $set1, $set2)->where($where)->get_compiled_update($table);
        } else {
            $sql = $this->db->set($set)->where($where)->get_compiled_update($table);
        }
        $query = $this->db->query($sql);
        return $query;
    }
}